﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using Terralever.Facebook.UI.Design;
using System.Globalization;

namespace Terralever.Facebook.UI.FbmlControls
{
    /// <summary>
    /// Displays content inside the tag only if the logged-in user is permitted by the specified privacy setting of the specified user.
    /// </summary>
    /// <remarks>
    /// <para>The Facebook documentation for the rendered control can be found 
    /// <a href="http://wiki.developers.facebook.com/index.php/Fb:if-can-see" target="_blank">here</a>.</para>
    /// </remarks>
    [ToolboxData("<{0}:IfCanSee runat=\"server\"></{0}:IfCanSee>")]
    [DefaultProperty("UserID")]
    public class IfCanSee : LogicTestControl
    {
        private UserProfilePrivacySetting m_what = UserProfilePrivacySetting.Search;

        /// <summary>
        /// Gets or sets the user ID to check.  This property is required.
        /// </summary>
        [Browsable(true)]
        [Category(FbmlConstants.FBML_REQD_ATTR)]
        [Description("Specifies the user ID to check.")]
        [DefaultValue(0)]
        [FbmlRequired(IsRequired = true)]
        public long UserID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the privacy setting to check for the specified user.
        /// </summary>
        /// <exception cref="InvalidEnumArgumentException">Thrown if the property is set to a value not defined by the <see>UserProfilePrivacySetting</see>
        /// enumeration.</exception>
        [Browsable(true)]
        [Category(FbmlConstants.FBML_OPT_ATTR)]
        [Description("Specifies the privacy setting to check.")]
        [DefaultValue(UserProfilePrivacySetting.Search)]
        public UserProfilePrivacySetting CanSeeWhat
        {
            get { return m_what; }
            set
            {
                if (!Enum.IsDefined(typeof(UserProfilePrivacySetting), value))
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(UserProfilePrivacySetting));
                m_what = value;
            }
        }

        /// <inheritdoc />
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);

            if (UserID == 0)
                throw new RequiredFbmlAttributeNotSetException("UserID", UserID);
            writer.AddAttribute("uid", UserID.ToString(CultureInfo.InvariantCulture));

            if (CanSeeWhat != UserProfilePrivacySetting.Search)
            {
                if (CanSeeWhat == UserProfilePrivacySetting.NotLimited)
                    writer.AddAttribute("what", "not_limited");
                else
                    writer.AddAttribute("what", CanSeeWhat.ToString().ToLowerInvariant());
            }
        }

        /// <inheritdoc />
        protected internal override string ElementName
        {
            get { return FbmlConstants.FB_IF_CAN_SEE; }
        }
    }
}
